package com.hu.zhcc.shiro.service.xy;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.common.entity.PageRequest;
import com.hu.zhcc.shiro.dao.lgx.TMemberMapper;
import com.hu.zhcc.shiro.dao.xy.TAssessAnswerMapper;
import com.hu.zhcc.shiro.dao.xy.TAssessQuestionOptionMapper;
import com.hu.zhcc.shiro.dao.xy.TMentalityResultMapper;
import com.hu.zhcc.shiro.entity.bo.base.Role;
import com.hu.zhcc.shiro.entity.dto.base.RoleDTO;
import com.hu.zhcc.shiro.entity.lgx.TMember;
import com.hu.zhcc.shiro.entity.xy.dto.TMResultDTO;
import com.hu.zhcc.shiro.entity.xy.pojo.TAssessAnswer;
import com.hu.zhcc.shiro.entity.xy.pojo.TAssessQuestion;
import com.hu.zhcc.shiro.entity.xy.pojo.TBodyType;
import com.hu.zhcc.shiro.entity.xy.pojo.TMentalityResult;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TMResultImpl implements TMResultService{

    @Autowired
    private TMentalityResultMapper mapper;
    @Autowired
    private TAssessQuestionOptionMapper tAssessQuestionOptionMapper;
    @Autowired
    private TAssessAnswerMapper tAssessAnswerMapper;
    @Autowired
    private TMemberMapper tMemberMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(TMentalityResult record) {
        return 0;
    }

    @Override
    @Transactional
    public TMResultDTO insertSelective(TMResultDTO dto) {
        TMentalityResult result = new TMentalityResult();
        BeanUtils.copyProperties(dto , result);
        this.mapper.insertSelective(result);
        dto.setId(result.getId());
        return dto;
    }


    @Override
    public TMResultDTO selectByPrimaryKey(Integer id) {
        TMentalityResult result = this.mapper.selectByPrimaryKey(id);
        if (result == null){
            return null;
        }
        TMResultDTO dto = new TMResultDTO();
        BeanUtils.copyProperties(result,dto);
        return dto;
    }

    @Override
    @Transactional
    public int updateByPrimaryKeySelective(TMResultDTO record) {
        TMentalityResult result = new TMentalityResult();
        BeanUtils.copyProperties(record , result);
        return this.mapper.updateByPrimaryKeySelective(result);
    }



    @Override
    public int updateByPrimaryKey(TMentalityResult record) {
        return 0;
    }

    @Override
    public Page<TMResultDTO> listTMResult(Map<String, Object> paremeters, int offset, int limit) {
        int total = this.mapper.countTMResult(paremeters);
        List<TMResultDTO> dtoList = null ;
        if (total>0){
            PageRequest pageRequest = new PageRequest(offset, limit, paremeters, null);
            List<TMentalityResult> list = this.mapper.listTMResult(pageRequest);
            dtoList = new ArrayList<TMResultDTO>(list.size());

            for (TMentalityResult tmr : list){
                TMResultDTO dto = new TMResultDTO();
                BeanUtils.copyProperties(tmr , dto);
                dtoList.add(dto);
            }

        }
        return new Page<TMResultDTO>(total,dtoList);
    }

    @Override
    public List<TMResultDTO> listAllTMResult() {
        List<TMentalityResult> list = this.mapper.listAllTMResult();
        ArrayList<TMResultDTO> dtoList = new ArrayList<>(list.size());
        for (TMentalityResult result :list){
            TMResultDTO dto = new TMResultDTO();
            BeanUtils.copyProperties(result , dto);
            dtoList.add(dto);
        }
        return dtoList;
    }

    @Override
    public Boolean queryByMemberId(Integer memberId) {
        TMentalityResult tMentalityResult = mapper.queryByMemberId(memberId);
        //tMentalityResult为空表示没有评估过，tMentalityResult的评估建议有值表示评估完成可以重新评估
        if(tMentalityResult==null||tMentalityResult.getEvaluateId()!=null){
            return false;
        }else{
            return true;
        }
    }

    @Override
    public Integer addTMentalityResult(String idCard,TAssessQuestion[][] objects) {
        TMember member = tMemberMapper.queryByIdCard(idCard);

        TMentalityResult tMentalityResult = new TMentalityResult();
        tMentalityResult.setTMemberId(member.getId());
        tMentalityResult.setName(member.getName());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tMentalityResult.setCreatetime(simpleDateFormat.format(new Date()));
        for(int i=0;i<objects.length;i++){
            Integer markTotal=0;

            TAssessQuestion[] tAssessQuestions=(TAssessQuestion[])objects[i];
            for(int j=0;j<tAssessQuestions.length;j++){
//                System.out.println(tAssessQuestions[j]);
                TAssessQuestion tAssessQuestion = tAssessQuestions[j];
                TAssessAnswer tAssessAnswer = new TAssessAnswer();
                tAssessAnswer.setUserId(member.getId());
                tAssessAnswer.setQuestionId(tAssessQuestion.getId());
//                tAssessAnswer.setAnswerId();
                //设置答案id
                if(tAssessQuestion.getIsRadio().equals("0")){
                    tAssessAnswer.setAnswerId(tAssessQuestion.getAnswers()+"");
//                System.out.println(tAssessQuestionOptionMapper.selectByPrimaryKey(tAssessQuestion.getSelectOption()).getMark());
                    markTotal+=tAssessQuestionOptionMapper.selectByPrimaryKey(Integer.parseInt(tAssessQuestion.getAnswers())).getMark();
                }else{
                    String options="";
                    for(int k=0;k<tAssessQuestion.getCheckOptions().size();k++){
                        options+=tAssessQuestion.getCheckOptions().get(k);
                        Double mark = Double.parseDouble(tAssessQuestion.getCheckOptions().get(k).toString());
//                    System.out.println(tAssessQuestionOptionMapper.selectByPrimaryKey(mark.intValue()).getMark());
                        markTotal+=tAssessQuestionOptionMapper.selectByPrimaryKey(mark.intValue()).getMark();
                        if(k<tAssessQuestion.getCheckOptions().size()-1){
                            options+=",";
                        }
                    }
                    tAssessAnswer.setAnswerId(options);
                }
                tAssessAnswerMapper.insertSelective(tAssessAnswer);
            }

            if(tAssessQuestions[0].getBankId()==13){
                //强迫症
                tMentalityResult.setTOcd(markTotal);
            }else if(tAssessQuestions[0].getBankId()==14){
                //焦虑
                tMentalityResult.setTAnxiety(markTotal);
            }else if(tAssessQuestions[0].getBankId()==15){
                //恐怖
                tMentalityResult.setTTerror(markTotal);
            }

        }
//        System.out.println(tMentalityResult);
        return mapper.insert(tMentalityResult);
    }
    @Override
    public Integer updateTMentalityResult(Integer memberId,TAssessQuestion[][] objects) {
        TMentalityResult tMentalityResult = mapper.selectByPrimaryKey(memberId);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tMentalityResult.setUpdatetime(simpleDateFormat.format(new Date()));

//        mapper.selectByPrimaryKey()
        for(int i=0;i<objects.length;i++){
            Integer markTotal=0;

            TAssessQuestion[] tAssessQuestions=(TAssessQuestion[])objects[i];
            for(int j=0;j<tAssessQuestions.length;j++){
//                System.out.println(tAssessQuestions[j]);
                TAssessQuestion tAssessQuestion = tAssessQuestions[j];
                TAssessAnswer tAssessAnswer = tAssessAnswerMapper.selectByPrimaryKey(tAssessQuestion.getAnswer_id());
                //设置答案id
                if(tAssessQuestion.getIsRadio().equals("0")){
                    tAssessAnswer.setAnswerId(tAssessQuestion.getAnswers());
//                System.out.println(tAssessQuestionOptionMapper.selectByPrimaryKey(tAssessQuestion.getSelectOption()).getMark());
                    markTotal+=tAssessQuestionOptionMapper.selectByPrimaryKey(Integer.parseInt(tAssessQuestion.getAnswers())).getMark();
                }else{
                    String options="";
                    for(int k=0;k<tAssessQuestion.getCheckOptions().size();k++){
                        options+=tAssessQuestion.getCheckOptions().get(k);
                        Double mark = Double.parseDouble(tAssessQuestion.getCheckOptions().get(k).toString());
//                    System.out.println(tAssessQuestionOptionMapper.selectByPrimaryKey(mark.intValue()).getMark());
                        markTotal+=tAssessQuestionOptionMapper.selectByPrimaryKey(mark.intValue()).getMark();
                        if(k<tAssessQuestion.getCheckOptions().size()-1){
                            options+=",";
                        }
                    }
                    tAssessAnswer.setAnswerId(options);
                }
//                System.out.println(tAssessAnswer);
                tAssessAnswerMapper.updateByPrimaryKeySelective(tAssessAnswer);
            }

            if(tAssessQuestions[0].getBankId()==13){
                //强迫症
                tMentalityResult.setTOcd(markTotal);
            }else if(tAssessQuestions[0].getBankId()==14){
                //焦虑
                tMentalityResult.setTAnxiety(markTotal);
            }else if(tAssessQuestions[0].getBankId()==15){
                //恐怖
                tMentalityResult.setTTerror(markTotal);
            }

        }
//        System.out.println(tBodyType);
        return mapper.updateByPrimaryKeySelective(tMentalityResult);

    }
}
